﻿using DataFiller.DataStorage.Extension;
using DataFiller.DataStorage.Extension.Excel.Holders;
using DataFiller.DataStorage.Extension.Excel.Locaters;
using DataFiller.DataStorage.Extension.ExcelOperator;
using DataFiller.DataStorage.Holders;
using DataFiller.DataStorage.Locaters;
using NPOI.SS.UserModel;

namespace DataFiller.ExportWithNpoi
{
    public class NCell : IExcelCell
    {
        private ICell _cell;

        public object CellStyle
        {
            get
            {
                return _cell.CellStyle;
            }
            set
            {
                _cell.CellStyle = (ICellStyle)value;
            }
        }

        public int RowIndex
        {
            get { return _cell.RowIndex; }
        }

        public NCell(ICell cell)
        {
            this._cell = cell;
        }

        //todo improve the code maintainability with the code below
        public void CellHandler(ExportContext context)
        {
            context.DataMap.FeedData(_cell.RowIndex, this.GetExcelHolder());
        }
        //todo improve the code maintainability with the code below
        public IDataHolder GetExcelHolder()
        {
            dynamic value = null;
            switch (_cell.CellType)
            {
                case CellType.BLANK:
                    break;
                case CellType.BOOLEAN:
                    value = _cell.BooleanCellValue;
                    break;
                case CellType.ERROR:
                    value = _cell.ErrorCellValue;
                    break;
                case CellType.FORMULA:
                    value = _cell.CellFormula;
                    break;
                case CellType.NUMERIC:
                    value = _cell.NumericCellValue;
                    break;
                case CellType.STRING:
                    value = _cell.StringCellValue;
                    break;
                default:
                    value = _cell.StringCellValue;
                    break;
            }
            //todo we should decouple with the code below 
            IHolderLocater locater = new ExcelLocater(_cell.RowIndex, _cell.ColumnIndex);
            return new ExcelHolder(locater)
            {
                Data = value,
                Style = _cell.CellStyle,
                Type = _cell.CellType
            };
        }
        //todo improve the code maintainability with the code below
        public void SetCellValue(ExcelHolder dsCell)
        {
            if ((CellType)dsCell.Type == CellType.FORMULA)
            {
                _cell.SetCellFormula((string)dsCell.Data);
                _cell.SetCellType(CellType.FORMULA);
            }
            else if (dsCell.Data is int)
            {
                _cell.SetCellValue((int)dsCell.Data);
                _cell.SetCellType(CellType.NUMERIC);
            }
            else if (dsCell.Data is bool)
            {
                _cell.SetCellValue((bool)dsCell.Data);
                _cell.SetCellType(CellType.BOOLEAN);
            }
            else if (dsCell.Data is string)
            {
                _cell.SetCellValue((string)dsCell.Data);
                _cell.SetCellType(CellType.STRING);
            }

            //switch ((CellType)dsCell.Type)
            //{
            //    case CellType.BLANK:
            //        break;
            //    case CellType.BOOLEAN:
            //        _cell.SetCellValue((bool)dsCell.Data);
            //        _cell.SetCellType(CellType.BOOLEAN);
            //        break;
            //    case CellType.ERROR:
            //        _cell.SetCellErrorValue((byte)dsCell.Data);
            //        _cell.SetCellType(CellType.ERROR);
            //        break;
            //    case CellType.FORMULA:
            //        _cell.SetCellFormula((string)dsCell.Data);
            //        _cell.SetCellType(CellType.FORMULA);
            //        break;
            //    case CellType.NUMERIC:
            //        _cell.SetCellValue((double)dsCell.Data);
            //        _cell.SetCellType(CellType.NUMERIC);
            //        break;
            //    case CellType.STRING:
            //        _cell.SetCellValue(dsCell.Data.ToString());
            //        _cell.SetCellType(CellType.STRING);
            //        break;
            //    default:
            //        break;
            //}
        }
    }
}
